package gw.com.android.ui.me;

import android.os.Bundle;
import android.text.TextUtils;

import com.gwtsz.android.rxbus.RxBus;

import org.json.JSONException;
import org.json.JSONObject;

import gw.com.android.app.AppContances;
import gw.com.android.model.ConfigType;
import gw.com.android.terminal.AppTerminal;
import gw.com.android.terminal.GTSDataListener;
import gw.com.jni.library.terminal.GTSConst;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import www.com.library.app.Logger;

/**
 * 获取用户上传的文件的状态：银行卡，身份证，视频等
 *
 * @author jett
 * @since 2018-11-16.
 */
public class UploadedFileStatus {

    private final String TAG = "UploadedFileStatus";
    private static UploadedFileStatus instance = new UploadedFileStatus();

    private final CompositeDisposable disposable = new CompositeDisposable();

    private UploadedFileStatus() {
    }

    public static UploadedFileStatus getInstance() {
        return instance;
    }

    public void request(final String username, final OnStateListener listener) {
        if (TextUtils.isEmpty(username)) {
            Logger.i(TAG, "username is empty");
            return;
        }
        Disposable mSubscribe = RxBus.getInstance().register(GTSConst.NORMALREQ_TYPE_GET_BANK_INFO, Bundle.class).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Bundle>() {
                    @Override
                    public void accept(Bundle bundle) {
                        Logger.i(TAG, "on customer file status respond");
                        parse(username, bundle, listener);
                        disposable.clear();
                    }
                });
        disposable.add(mSubscribe);
        Logger.i(TAG, "request customer file status...");
        int mSeq = GTSDataListener.curSeq++;
        GTSDataListener.instance().addSeqList(mSeq + "", AppContances.NORMALREQ_TYPE_GET_BANK_INFO);
        AppTerminal.instance().NormalReqConfigSvr(ConfigType.HTTP_CUSTOMERFILESTATUS, "", mSeq);
    }

    private void parse(String username, Bundle bundle, OnStateListener listener) {
        if (bundle != null) {
            String strObject = bundle.getString("strObject");
            if (!TextUtils.isEmpty(strObject)) {
                try {
                    JSONObject jsonObject = new JSONObject(strObject);
                    JSONObject resultJson = jsonObject.optJSONObject("result");
                    if (resultJson != null) {
                        JSONObject frontJson = resultJson.optJSONObject("FILE_TYPE_IDCARD_FRONT");
                        JSONObject backJson = resultJson.optJSONObject("FILE_TYPE_IDCARD_BACK");
                        if (frontJson != null && backJson != null) {
                            boolean hasIdcardFront = "APPROVAL".equals(frontJson.optString("status"));
                            boolean hasIdcardBack = "APPROVAL".equals(backJson.optString("status"));
                            Logger.i(TAG, "parse result.. hasIdcardFront = " + hasIdcardFront + "  hasIdcardBack = " + hasIdcardBack);
                            boolean authen = hasIdcardFront && hasIdcardBack;
                            if (listener != null) {
                                listener.onResponse(authen);
                            }
                        } else {
                            if (listener != null) {
                                listener.onResponse(false);
                            }
                        }
                    }
                } catch (JSONException e) {
                    Logger.i(TAG, "strObject = " + strObject);
                    Logger.e(e);
                }
            }
        }
    }

    public interface OnStateListener {
        void onResponse(boolean approved);
    }

}
